/* @formatter:off
 * Copyright (c) 2011 Lucas <lucas03093@gmail.com>
 * 
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
/* @formatter:on */

package nu.korkad.daytracker.info;

import nu.korkad.daytracker.R;
import nu.korkad.daytracker.settings.PrefStore;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Network
{
    /**
     * Checks for networks depending on user settings.
     * 
     * @param context
     * @return "ok" if check is ok or error message if there was an error.
     */
    public static String networkCheck(Context context)
    {
        // Check for available wifi network
        if ((PrefStore.getWifiOnly(context) && Network.isWifiAvailable(context))
        // Check for available networks
                || (!PrefStore.getWifiOnly(context) && Network.isNetworkAvailable(context)))
            return "ok";
        
        // Set wifi error message
        else if (PrefStore.getWifiOnly(context) && !Network.isWifiAvailable(context))
            return context.getString(R.string.network_error_nowifi);
        
        // Set network error message
        else if (!PrefStore.getWifiOnly(context) && !Network.isNetworkAvailable(context))
            return context.getString(R.string.network_error_none);
        
        // This shouldn't happen
        return "fail";
    }
    
    /**
     * Check for connected network.
     * 
     * @param context
     * @return true if a network is connected.
     */
    public static boolean isNetworkAvailable(Context context)
    {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        
        // Go through all networks to find one that is connected
        if (cm != null)
        {
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null)
                for (NetworkInfo element : info)
                    if (element.getState() == NetworkInfo.State.CONNECTED)
                        return true;
        }
        
        // Otherwise return false
        return false;
    }
    
    /**
     * Check if WiFi is connected.
     * 
     * @param context
     * @return true if WiFi is connected.
     */
    public static boolean isWifiAvailable(Context context)
    {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        
        // Check if wifi is connected
        if (cm.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)
            return true;
        
        // Otherwise return false
        return false;
    }
}
